package com.kxxnzstdsw.modle.repo.impl

import com.kxxnzstdsw.modle.entity.Account
import com.kxxnzstdsw.modle.entity.username
import com.kxxnzstdsw.modle.repo.AccountRepository
import com.kxxnzstdsw.modle.repo.BasicRepository
import org.babyfish.jimmer.sql.kt.KSqlClient
import org.babyfish.jimmer.sql.kt.ast.expression.eq
import java.sql.Connection

class AccountRepositoryImpl(override val sqlClient: KSqlClient) : AccountRepository, BasicRepository() {
  override suspend fun info(id: Long) = sqlClient.findById(Account::class, id)


  override suspend fun infoByUsername(username: String) =
    sqlClient.createQuery(Account::class) {
      where(table.username eq username)
      select(table)
    }.fetchOneOrNull()

  override suspend fun save(account: Account, conn: Connection?) {
    sqlClient.entities.save(account, conn)
  }

}
